export default defineNuxtPlugin(() => {
  const $request = $fetch.create({
    // 请求拦截器
    onRequest({ options }) {
      const newOptions = {
        ...options,
      };

      const userStore = useStore.userStore();
      const appStore = useStore.appStore();
      let token = userStore.token;
      let tenantId = userStore.tenantId;
      
      // 使用响应式获取语言值
      const { language } = storeToRefs(appStore);
      let currentLanguage = language.value || appStore.language;
      newOptions.headers = {
        ...newOptions.headers,
        authorization: `Bearer ${ token }`,
        "Accept-Language": currentLanguage,
        "Tenant-Id": tenantId,
      };
    },
    // 响应拦截器
    onResponse({ response }) {
      if (response.status >= 200 && response.status < 300) {
        response._data = response._data ?? null;
      }
    },
    // 请求错误拦截器
    onRequestError({ request, error }) { },
    // 响应错误拦截器
    onResponseError(_context) { },
  });
  return {
    provide: {
      request: $request,
    },
  };
});
